<?php

namespace App\Console\Commands;

use Encore\Admin\Admin;
use Encore\Admin\Auth\Database\Administrator;
use Illuminate\Console\Command;
use DB;

class AddApplys extends Command {

    protected $signature = 'add:applys';
    protected $description = '学员流失脚本';

    public function __construct(){
        parent::__construct();
    }
	/**
	 * [判断处理方式]
	 */
    public function handle() {
		DB::beginTransaction();
		try {

			$applys = DB::table("applys")->where("status","=","1")
			->whereIn("type",["2","3"])
			->pluck('student_id');
			$applys = json_decode(json_encode($applys),true);
			$st = DB::table("students")->whereIn('status',["2","3"])
			->whereNotIn("id",$applys)->get();
			$st = json_decode(json_encode($st),true);
			$bar = $this->output->createProgressBar(sizeof($st));
			foreach($st as $s){
				DB::table("applys")->insert([
					"type" => $s['status'],
					"status" => "1",
					"student_id" => $s['id'],
					"squad_id" => $s["squad_id"],
					"op_id" => 1,
					"sp_id" => 1,
					"sp_time" => "2019-02-18 23:59:59",
					"created_at" => "2019-02-18 23:59:59",
					"updated_at" => "2019-02-18 23:59:59",
					"memo" => "脚本添加",
					"term_year" => 2019,
					"term_season" => 1,
					"week" => "1"
				]);
				$bar->advance();
			}
			$bar->finish();
			DB::commit();
			echo "\n Success\n";
		} catch (\Exception $e) {
			DB::rollback();
			echo "\n Failed:".$e->getMessage()."\n";
		}
    }
}
